package com.lottery.system.common.utils;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
import org.apache.commons.codec.digest.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.util.UUID;

/**
 * @author: xiaoxie
 * create: 2024-09-02 16:37
 * @BelongsProject: lottery-system
 * @BelongsPackage: com.lottery.system.common.utils
 * description: 加密工具类
 */
public class SecretUtil {
    /**
     * @description: 生成随机的uuid作为盐值
     * @author: xiaoxie
     * @date:  16:38
     * @param: []
     * @return: java.lang.String
     **/
    public static String getSalt() {
        return UUID.randomUUID().toString().replace("-", "");
    }
    /**
     * @description: 对输入的明文进行md5加密
     * @author: xiaoxie
     * @date: 2024/9/2 16:43
     * @param: [input]
     * @return: java.lang.String
     **/
    private static String md5(String input) {
        return DigestUtils.md5Hex(input);
    }
    /**
     * @description: 密码和盐进行md5加密
     * @author: xiaoxie
     * @date: 2024/9/2 16:47
     * @param: [password, salt]
     * @return: java.lang.String
     **/
    public static String passwordAndSaltByMd5(String password, String salt) {
        return DigestUtils.md5Hex(md5(password) + salt);
    }
    /**
     * @description: 将数据库的密文存储的密码和输入的密码进行校验
     * @author: xiaoxie
     * @date: 2024/9/2 16:48
     * @param: [password, salt, sql]
     * @return: java.lang.Boolean
     **/
    public static Boolean verify(String password, String salt,String sql) {
        String s = passwordAndSaltByMd5(password, salt);
        return s.equalsIgnoreCase(sql);
    }
    private final static byte[] KEY = "qwesvfgdsdsg0865".getBytes(StandardCharsets.UTF_8);
    public static String encryptByAes(String data) {
        AES aes = SecureUtil.aes(KEY);
        return aes.encryptHex(data);
    }
    public static String decryptByAes(String data) {
        AES aes = SecureUtil.aes(KEY);
        return aes.decryptStr(data);
    }

}
